import { useEffect } from 'react';
import { useNavigate } from 'react-router-dom';

import { useAuth } from 'hooks/auth';

import { IAM_KEY } from '../constants';

export default function Login() {
  const { loginWithRedirect, isAuthenticated } = useAuth();
  const navigate = useNavigate();

  useEffect(() => {
    if (!isAuthenticated) {
      loginWithRedirect({
        response_type: 'token',
        client_id: IAM_KEY,
        scope: 'all',
        redirect_uri: `${window.location.origin}/api/auth/callback`
        // authorizationParams: {
        //   audience: 'chainlit-cloud'
        // }
      });
    } else navigate('/');
  }, [isAuthenticated]);

  return null;
}
